﻿using IOP.ISEA;
using IOP.SgrA;
using IOP.SgrA.SilkNet.Vulkan;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text.Json;
using System.Threading.Tasks;

namespace VkSample64
{
    class Program
    {
        static async Task Main(string[] args)
        {
            CreateISEA(true);
            var h = Host.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration((config) =>
                {
                    config.SetBasePath(Directory.GetCurrentDirectory());
                    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) config.AddVulkanJsonFile("Vulkan.json");
                    else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) config.AddVulkanJsonFile("Vulkan-Linux.json");
                    else throw new NotSupportedException("Not supported Platform");
                })
                .ConfigureSgrAHost((sgra) =>
                {
                    sgra.AddVulkanEnvironmental();
                    sgra.UseStartup<Startup>();
                }).ConfigureLogging((logger) => logger.AddConsole()); ;
            await h.RunConsoleAsync();
        }

        static void CreateISEA(bool isNeed)
        {
            var texPath = Path.Combine(AppContext.BaseDirectory, "Assets", "Textures");
            var imageJson = JsonDocument.Parse(File.ReadAllText(Path.Combine(texPath, "chessboard.json")));
            var chessboardJson = imageJson.RootElement.GetProperty("Chessboard").GetRawText();
            var samplerJson0 = imageJson.RootElement.GetProperty("Sampler0").GetRawText();
            var samplerJson1 = imageJson.RootElement.GetProperty("Sampler1").GetRawText();
            ISEADocument document = new ISEADocument();
            ConfigNode chessboardConfig = document.CreateConfigNode("ChessboardJson", chessboardJson, ConfigEngine.Vulkan);
            ConfigNode sampler0 = document.CreateConfigNode("Sampler0", samplerJson0, ConfigEngine.Vulkan);
            ConfigNode sampler1 = document.CreateConfigNode("Sampler1", samplerJson1, ConfigEngine.Vulkan);
            IOP.ISEA.ImageNode linear256 = document.CreateImageNode("Linear256", Path.Combine(texPath, "256Linear.bntex"));
            IOP.ISEA.ImageNode nearest256 = document.CreateImageNode("Nearest256", Path.Combine(texPath, "256Nearest.bntex"));
            IOP.ISEA.ImageNode linear32 = document.CreateImageNode("Linear32", Path.Combine(texPath, "32Linear.bntex"));
            IOP.ISEA.ImageNode nearest32 = document.CreateImageNode("Nearest32", Path.Combine(texPath, "32Nearest.bntex"));
            List<float[]> vDatas = new List<float[]>(2);
            vDatas.Add(new float[]
            {
                2,2,0,1,0, -2,2,0,0,0, -2,-2,0,0,1,
                2,2,0,1,0, -2,-2,0,0,1, 2,-2,0,1,1
            });
            vDatas.Add(new float[]
            {
                7,7,0,1,0, -7,7,0,0,0, -7,-7,0,0,1,
                7,7,0,1,0, -7,-7,0,0,1, 7,-7,0,1,1
            });
            MeshNode mesh1 = document.CreateEmptyMeshNode($"Board32Mesh", 6)
                .AddMeshData(vDatas[0], 20, 0, MeshDataPurpose.COMPLEX, false);
            MeshNode mesh2 = document.CreateEmptyMeshNode($"Board256Mesh", 6)
                .AddMeshData(vDatas[1], 20, 0, MeshDataPurpose.COMPLEX, false);
            TextureNode tex032 = document.CreateTextureNode($"Board32Tex0", TextureUsage.CombinedImageSampler, linear32,
                chessboardConfig, sampler0, 1);
            TextureNode tex132 = document.CreateTextureNode($"Board32Tex1", TextureUsage.CombinedImageSampler, nearest32,
                chessboardConfig, sampler1, 1);
            TextureNode tex0256 = document.CreateTextureNode($"Board256Tex0", TextureUsage.CombinedImageSampler, linear256,
                chessboardConfig, sampler0, 1);
            TextureNode tex1256 = document.CreateTextureNode($"Board256Tex1", TextureUsage.CombinedImageSampler, nearest256,
                chessboardConfig, sampler1, 1);
            document.CreateRenderObjectNode($"Board32", mesh1, tex032, tex132);
            document.CreateRenderObjectNode($"Board256", mesh2, tex0256, tex1256);
            document.Save(AppContext.BaseDirectory, "Test64");
        }
    }
}
